import allure
from appium.webdriver.common.appiumby import AppiumBy

from base.wework_app import Wework


class AddMemberPage(Wework):
    """
    添加成员页面
    """

    # 手动输入添加按钮
    _add_member_btn = (AppiumBy.XPATH, "//*[@text='手动输入添加']")
    # 姓名文本框
    _name_text = (AppiumBy.XPATH, "//*[@text='姓名']/following-sibling::*[@class='android.widget.EditText']")
    # 手机文本框
    _phone_text = (AppiumBy.XPATH, "//*[@text='手机']//following-sibling::*//*[@class='android.widget.EditText']")
    # 保存按钮
    _save_btn = (AppiumBy.XPATH, "//*[@text='保存']")
    # 异常提示框
    _error_tips = (AppiumBy.XPATH, "//*[@class='android.widget.ScrollView']/*")

    @allure.step("点击手动输入添加按钮")
    def click_add_member(self):
        """
        点击手动输入添加按钮
        :return:
        """
        self.find_ele(self._add_member_btn).click()
        return self

    @allure.step("录入并保存成员信息")
    def save_member_info(self, name, phone):
        """
        录入并保存成员信息
        :param name: 姓名
        :param phone: 手机号
        :return:
        """
        # 录入姓名和手机
        self.find_ele(self._name_text).send_keys(name)
        self.find_ele(self._phone_text).send_keys(phone)
        # 点击保存
        self.find_ele(self._save_btn).click()
        return self

    @allure.step("获取异常提示")
    def get_error_tips(self):
        """
        获取异常提示
        :return:
        """
        return self.find_ele(self._error_tips).text

    @allure.step("返回通讯录页面")
    def goto_address_list_page(self):
        """
        返回通讯录
        :return:
        """
        from page.address_list_page import AddressListPage
        self.go_back(1)
        return AddressListPage(self.driver)
